import javax.sound.sampled.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class SoundClient implements ActionListener {
    Socket requestSocket;
    ObjectOutputStream out;
    ObjectInputStream in;
    ConnectionOptions connectionOptions;

    Chat chat;

    static class ConnectionOptions extends JFrame implements ActionListener{
        private JButton m_connectButton;
        private JButton m_closeButton;

        private JTextField m_ipField;
        private JTextField m_codeField;

        public ConnectionOptions()
        {
            super("Connection options");

            Container pane = getContentPane();
            pane.setLayout(new GridBagLayout());
            
            GridBagConstraints c = new GridBagConstraints();
            c.insets = new Insets(5, 5, 5, 5);
            c.fill = GridBagConstraints.CENTER;
            c.gridx = 0;      c.gridy = 2;
            c.gridwidth = 1;  c.gridheight = 1;
            c.weightx = 1.0;  c.weighty = 0.0;
            m_connectButton = new JButton("Connect");
            pane.add(m_connectButton, c);

            c.fill = GridBagConstraints.CENTER;
            c.gridx = 1;      c.gridy = 2;
            c.gridwidth = 1;  c.gridheight = 1;
            c.weightx = 1.0;  c.weighty = 0.0;
            m_closeButton = new JButton("Close");
            m_closeButton.addActionListener(this);
            pane.add(m_closeButton, c);

            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 0;      c.gridy = 0;
            c.gridwidth = 2;  c.gridheight = 1;
            c.weightx = 1.0;  c.weighty = 0.0;
            m_ipField = new JTextField("localhost");
            m_ipField.setBorder(new TitledBorder("IP-address"));
            pane.add(m_ipField, c);

            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 0;      c.gridy = 1;
            c.gridwidth = 2;  c.gridheight = 1;
            c.weightx = 1.0;  c.weighty = 0.0;
            m_codeField = new JTextField("8020");
            m_codeField.setBorder(new TitledBorder("Port"));
            pane.add(m_codeField, c);

            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setResizable(false);
            setVisible(true);
            setSize(200,170);
        }

        public void actionPerformed(ActionEvent e)
        {
            dispose();
        }
        
        public void addListener(ActionListener actionListener)
        {
            m_connectButton.addActionListener(actionListener);
        }
        
        public String getIP()
        {
            return m_ipField.getText();
        }
        
        public int getPort()
        {
            return Integer.parseInt(m_codeField.getText());
        }
    }

    SoundClient()
    {
        connectionOptions = new ConnectionOptions();
        connectionOptions.addListener(this);
        //chat = new Chat("Client");
        //chat.writeSystemMessage("Start client application");
    }
    
    public void actionPerformed(ActionEvent e)
    {
        String ip = connectionOptions.getIP();
        int port = connectionOptions.getPort();
        connectionOptions.dispose();
        chat = new Chat("Client");
        chat.writeSystemMessage("Start client application");
        start(ip, port);
    }

    public void start(String ip, int port)
    {
        chat.writeSystemMessage("Waiting for connection...");
        do {
            try{
                requestSocket = new Socket(ip, port);
                out = new ObjectOutputStream(requestSocket.getOutputStream());
                out.flush();
                in = new ObjectInputStream(requestSocket.getInputStream());
            }
            catch(UnknownHostException unknownHost){
                chat.writeSystemMessage("You are trying to connect to an unknown host!");
            }
            catch(IOException ioException){
                ioException.printStackTrace();
            }
        } while (in == null || out == null);

        chat.writeSystemMessage("Connected to " + requestSocket.getInetAddress().toString() + " in port " + requestSocket.getPort());
        chat.setIn(in);
        chat.setOut(out);
        chat.start();
    }


    public static void main(String args[])
    {
        SoundClient client = new SoundClient();
        //client.start();
    }
}

